﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
#endregion

namespace TunnelsOfDeath
{
    class GUI
    {
        public bool ShouldDrawMap { get; set; }
        private double gameOverTimer = 0;
        private float gameOverTransparency = 0;
        private double nextMapTimer = 0;

        private Texture2D texGameOver;
        private Texture2D startScreen;
        private SpriteFont spriteFont;
        private Texture2D keyboard;
        private Texture2D gamepad;

        public GUI(ContentManager _content)
        {
            ShouldDrawMap = true;
            texGameOver = _content.Load<Texture2D>("gameover");
            startScreen = _content.Load<Texture2D>("start");
            spriteFont = _content.Load<SpriteFont>("SpriteFont1");
            keyboard = _content.Load<Texture2D>("keyboard");
            gamepad = _content.Load<Texture2D>("gamepad");
            
        }

        public void Draw(SpriteBatch _sp)
        
        {
            if (Globals.CurrentState == GameState.gameover)
            {
                DrawGameOver(_sp); 
            }

            if (Globals.CurrentState == GameState.startscreen)
            {
                
                DrawStart(_sp);
            }

            if (Globals.CurrentState == GameState.game || Globals.CurrentState == GameState.levellost)
            {
                DrawGUI(_sp);
            }
        }

        protected void DrawGUI(SpriteBatch _sp)
        {
            _sp.DrawString(spriteFont, "Lives: " + Globals.MainPlayer.Lives.ToString(), new Vector2(0, 0), Color.White);
            _sp.DrawString(spriteFont, "Score: " + Globals.MainPlayer.Scores.ToString(), new Vector2(300*Globals.ScreenRatio, 0), Color.White);
        }

        protected void DrawStart(SpriteBatch _sp)
        {
            _sp.Draw(startScreen, new Vector2(0, 0), new Rectangle(0, 0, Globals.Screens[0].Width, Globals.Screens[0].Height), Color.White, 0f, new Vector2(0, 0), Globals.ScreenRatio, SpriteEffects.None, 1f);
            _sp.DrawString(spriteFont, "Press Start on your gamepad\nor\nEnter on your keyboard", new Vector2(1 * Globals.ScreenRatio, 1 * Globals.ScreenRatio), Color.Yellow,0f,new Vector2(0,0),3,SpriteEffects.None,1f);
        
        }

        protected void DrawGameOver(SpriteBatch _sb)
        {
            if (gameOverTimer != 0)
            {
                Vector2 position = new Vector2(100+Globals.ScreenFixX, 25+Globals.ScreenFixY);
                position *= Globals.ScreenRatio;
                _sb.Draw(texGameOver, position, new Color(Color.White,gameOverTransparency));
            }
        }

        public void Update(GameTime _gt)
        {

            GameOverUpdate(_gt);
            NextMapUpdate(_gt);
            UpdateStartScreen(_gt);
        }

        private void NextMapUpdate(GameTime _gt)
        {
            //TODO
            if (Globals.CurrentState == GameState.levelpassed)
            {
                Globals.CurrentState = GameState.startup;
            }
        }

        private void UpdateStartScreen(GameTime _gt)
        { //Start screen options here
            if (Globals.CurrentState == GameState.startscreen)
            {
                Globals.CurrentMapName = "";

                //Start keys
                if (GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed)
                {
                    Globals.MainPlayer = new PlayerData();
                    Globals.CurrentState = GameState.startup;
                    Globals.MainPlayer = new PlayerData();
                    //TODO change PlayerData constructor so it can be added faster;
                    Globals.MainPlayer.Controller = Objects.ControllerSource.GAMEPAD1;
                    Globals.MainPlayer.Lives = 3;
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    Globals.CurrentState = GameState.startup;
                    Globals.MainPlayer = new PlayerData();
                    //TODO change PlayerData constructor so it can be added faster;
                    Globals.MainPlayer.Controller = Objects.ControllerSource.KEYBOARD;
                    Globals.MainPlayer.Lives = 3;
                }
            }
        }

        private void GameOverUpdate(GameTime _gt)
        {
            if (Globals.CurrentState == GameState.gameover)
            {
                if (gameOverTimer == 0)
                {
                    gameOverTimer = _gt.TotalGameTime.TotalMilliseconds + 5000;
                }
                else
                {
                    if (gameOverTimer < _gt.TotalGameTime.TotalMilliseconds)
                    {
                        ShouldDrawMap = false;
                    }
                    else
                    {
                        double elapsedTime = 5000 - (gameOverTimer - _gt.TotalGameTime.TotalMilliseconds);
                        double transp = (elapsedTime / 5000);
                        gameOverTransparency = (float)transp;

                    }

                }
            }
            else
            {
                gameOverTimer = 0;
                gameOverTransparency = 0;
            }
        }

    }
}
